<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Cache;
class Document extends Model
{
    protected $connection = 'mysql_pclady';
    protected $table = 'document';

    public function getArticleRand($num){
        $ids = getNumberRand(1,$this->count(),$num);
        $res = $this->whereIn('document.id',$ids)->select('document.id','title','description','cdn_pic as path','document.create_time')
            ->join('picture', 'document.cover_id', '=', 'picture.id')
            ->get();
        foreach ($res as $key => $obj){
            $obj->title = preg_replace('/_(.*?)/','',$obj->title);
        }
        return $res;
    }
    public function getListsCount($cate = 0){
        $cacheName = md5("getListsCount".$cate);
        if (!Cache::has($cacheName)) {
            $category = new Category();
            $cateIds = $category->getChildrenId($cate);
            $res = $this->whereIn('category_id',$cateIds)->count();
            Cache::forever($cacheName, $res);
        }
        return Cache::get($cacheName);
    }
    public function getLists($cate = 0,$page = 0,$pageSize = 10){
        $cacheName = md5("getLists".$cate);
        if (!Cache::has($cacheName)) {
            $category = new Category();
            $cateIds = $category->getChildrenId($cate);
            $res = $this->offset($page * $pageSize)->limit($pageSize)->whereIn('category_id',$cateIds)->select('document.id','title','description','cdn_pic as path','document.create_time')
                ->join('picture', 'document.cover_id', '=', 'picture.id')
                ->get();

            foreach ($res as $key => $obj){
                $obj->title = preg_replace('/_(.*?)/','',$obj->title);
            }
            $res = $res->toArray();
            Cache::forever($cacheName, $res);
        }
        return Cache::get($cacheName);
    }

    public function getArticleData($cate = 0){
        $cacheName = md5("getArticleData");
        if (!Cache::has($cacheName)) {
            $ids = getNumberRand(1,$this->count(),150);
            $res = $this->whereIn('document.id',$ids)->select('document.id','title','description','cdn_pic as path','document.create_time')
                ->join('picture', 'document.cover_id', '=', 'picture.id')
                ->get();
            foreach ($res as $key => $obj){
                $obj->title = preg_replace('/_(.*?)/','',$obj->title);
            }
            $res = $res->toArray();
            Cache::forever($cacheName, $res);
        }
        $res =  Cache::get($cacheName);
        $data['list1'] =array_chunk($res,1);
        $data['list2'] =array_chunk($res,2);
        $data['list3'] =array_chunk($res,3);
        $data['list4'] =array_chunk($res,4);
        $data['list5'] =array_chunk($res,5);
        $data['list6'] =array_chunk($res,6);
        $data['list7'] =array_chunk($res,7);
        $data['list8'] =array_chunk($res,8);
        $data['list9'] =array_chunk($res,9);
        $data['list10'] =array_chunk($res,10);
        return $data;
    }
}
